package demo;
//动物类
class Animal{
    protected String name;
    protected String gender;

    public Animal(String name, String gender) {
        this.name = name;
        this.gender = gender;
    }

    public void eat(){
        System.out.println("吃饭");
    }
}
//狗类继承动物类
class Dog extends Animal{
    public Dog(String name, String gender) {
        super(name, gender);
    }

    public void eat(){
        System.out.println("吃饭啦啦啦");
    }
}
//猫类继承动物类
class Cat extends Animal{
    public Cat(String name, String gender) {
        super(name, gender);
    }

    public void eat(){
        System.out.println("吃饭呼呼呼");
    }
}
public class Test {
    public static void main(String[] args) {
        Dog dog=new Dog("大胖","公");
        Cat cat=new Cat("阿猫","公");
        //向上转型
        Animal animal=cat;
        animal.eat();

        //再向下转型
        if(animal instanceof Dog){ //若实例化为狗则转化为狗
            dog=(Dog)animal;
        }
        if(animal instanceof Cat){ //若实例化为猫则转化为猫
            cat=(Cat)animal;
        }
    }

}
